#ifndef NODE_H
#define NODE_H
#include <QObject>

#include <string>
#include <QTime>
#include "tools/constant.h"




struct Value {

    Value():m_falg(0),
        m_lastValue(0){}

    /**
     * @brief m_falg 值的类型描述
     */
    int m_falg;

    /**
     * @brief m_lastValue 发送的坏值
     */
    int m_lastValue;

    /**
     * @brief m_lastTime 这个节点的最近一次发送信息的时间点
     */
    QTime m_lastTime;
};



/**
 * @brief The Node class
 * 对网络中节点的封装
 *
 *
 */




class Node
{
public:
    explicit Node();
    ~Node(){}

    Node(const Node &c):
        isVaild(c.isVaild),
        m_address(c.m_address),
        m_parentAdress(c.m_parentAdress),
        m_name(c.m_name),
        m_description(c.m_description),
        m_Devdescribe(c.m_Devdescribe),
        isActive(c.isActive),
        isTest(c.isTest),
        m_weight(c.m_weight){
    }

    Node& operator =(const Node &n) {
        isVaild = n.isVaild;
        m_address = n.m_address;
        m_parentAdress = n.m_parentAdress;
        m_name = n.m_name;
        m_description = n.m_description;
        m_Devdescribe = n.m_Devdescribe;
        isActive = n.isActive;
        isTest = n.isTest;
        m_weight = n.m_weight;
        return *this;
    }

    const QString& getAddress() const;
    void setAddress(const QString& ad);

    const QString& getIEEE() const;
    void setIEEE(const QString& ie);

    const QString& getParentAd() const;
    void setParentAd(const QString& pa);

    const QString &getParentIEEE() const;
    void setParentIEEE(const QString& paie);

    ushort getName() const;
    void setName(ushort na);

    NodeType getDescription() const;
    void setDescription(NodeType type);

    bool getState() const;

    void setState(bool bl) {
        isActive = bl;
    }

    const QString& getDevDescription() const;
    void setDevDescription(const QString & des);

    QByteArray getCMD(quint8 cmdC) const;//将cmdC参数传入，加上命令的头部，返回一个字节数组
    QByteArray getCMDIEEE(quint8 cmdc) const;

    void m_weightAddSelfByOne() {
        ++m_weight;
    }
    void m_weightClear0() {
        m_weight = 0;
    }
    unsigned int getM_weight() const {
        return m_weight;
    }
    void makeLost() {
        isActive = false;
    }
    void makeNotLost() {
        isActive = true;
    }

    bool NowIsTest() const {
        return isTest;
    }

    void NowTest() {
        isTest = true;
    }

    void NowNotTest() {
        isTest = false;
    }

    static int itoa(int val, char *buf, int radix);

    bool operator ==(const Node &o);

    bool isVaild;

    static const QString getFlagInfo(int flag);
    /**
     * @brief getValues
     * @return
     */
    QList<Value>& getValues() {
        return m_value;
    }
    /**
     * @brief getValue
     * @param flag 数据的标识
     * @return
     */
    Value& getValue(int flag) {
        QList<Value>::iterator ite = m_value.begin();
        for (;ite != m_value.end();++ite) {
            if ((*ite).m_falg == flag) {
                return (*ite);
            }
        }
        m_v.m_falg = -1;
        return m_v;
    }

    bool hasFlag(int flag) const{
        foreach (const Value &t, m_value) {
            if (t.m_falg == flag) {
                return true;
            }
        }
        return false;
    }

    void addValue(const Value &v) {
        m_value.append(v);
    }

private:
    QString m_address;//16位短地址
    //QString m_IEEE;//64位IEEE地址
    QString m_parentAdress;//父节点的16位短地址
    // QString m_parentIEEE;//父节点的64位IEEE地址
    ushort m_name;//设备名
    NodeType m_description;//节点描述符
    /**
     * @brief m_describe 设备信息描述，需要根据设备名到数据库中查找
     */
    QString m_Devdescribe;
    bool isActive;//标识此节点是否是活动的
    bool isTest;//节点丢失测试表示

    unsigned int m_weight;//权值

    /**
     * @brief m_value 记录各个相关值的发送情况
     */
    QList<Value> m_value;

    /**
     * @brief m_v 无效
     */
    Value m_v;









    
};

#endif // NODE_H
